package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Teacher;
import com.dinggou.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Resource(name="teacherServiceImpl")  //这个注解表示自动注入adminSeerviceImpl业务实现类
    @Autowired      //该注解表示后紧跟的变量进行封装，即自动产生getter和setter
    private TeacherService teacherService;

    /****
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(teacherService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    /****
     * 添加
     * @param record
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Teacher record){

        Teacher teacher = new Teacher();
        teacher.setName(record.getName());
        teacher.setPhone(record.getPhone());
        teacher.setEmail(record.getEmail());
        teacher.setQq(record.getQq());
        teacher.setDepartId(record.getDepartId());
        teacher.setDepartName(record.getDepartName());
        teacher.setPass(record.getPass());
        teacher.setQuestion(record.getQuestion());
        teacher.setAnswer(record.getAnswer());
        teacher.setUtype(record.getUtype());

        if(teacherService.insert(teacher)>0){
            return ServerResponse.createBySuccess("添加数据成功",teacher);
        }else{
            return ServerResponse.createByError();
        }
    }


    /****
     * 多个添加
     * @param record
     * @return
     */
    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Teacher record){
        Teacher teacher = new Teacher();
        teacher.setName(record.getName());
        teacher.setPhone(record.getPhone());
        teacher.setEmail(record.getEmail());
        teacher.setQq(record.getQq());
        teacher.setDepartId(record.getDepartId());
        teacher.setDepartName(record.getDepartName());
        teacher.setPass(record.getPass());
        teacher.setQuestion(record.getQuestion());
        teacher.setAnswer(record.getAnswer());
        teacher.setUtype(record.getUtype());

        if(teacherService.insert(teacher)>0){
            return ServerResponse.createBySuccess("添加数据成功",teacher);
        }else{
            return ServerResponse.createByError();
        }
    }

    /****
     * 通过id查询
     * @param id
     * @return
     */
    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id){
        Teacher teacher = teacherService.selectByPrimaryKey(id);

        if(teacher != null){
            return ServerResponse.createBySuccess(1,teacher);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }

    /****
     * 查询所有
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){

        List<Teacher> teacher=teacherService.selectAll();
        if(teacher.size()>0){
            return ServerResponse.createBySuccess(teacher.size(),teacher);
        }else{
            return ServerResponse.createByErrorMessage("找不到部门单位信息");
        }
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Teacher record){
        Teacher teacher = new Teacher();
        teacher.setId(record.getId());
        teacher.setName(record.getName());
        teacher.setPhone(record.getPhone());
        teacher.setEmail(record.getEmail());
        teacher.setQq(record.getQq());
        teacher.setDepartId(record.getDepartId());
        teacher.setDepartName(record.getDepartName());
        teacher.setPass(record.getPass());
        teacher.setQuestion(record.getQuestion());
        teacher.setAnswer(record.getAnswer());
        teacher.setUtype(record.getUtype());

        if(teacherService.updateByPrimaryKeySelective(teacher)>0){
            return ServerResponse.createBySuccess("更新数据成功",teacher);
        }else{
            return ServerResponse.createByErrorMessage("更新数据失败");
        }
    }


    /****
     * 通过id更新
     * @param record
     * @return
     */
    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Teacher record){
        Teacher teacher = new Teacher();
        teacher.setId(record.getId());
        teacher.setName(record.getName());
        teacher.setPhone(record.getPhone());
        teacher.setEmail(record.getEmail());
        teacher.setQq(record.getQq());
        teacher.setDepartId(record.getDepartId());
        teacher.setDepartName(record.getDepartName());
        teacher.setPass(record.getPass());
        teacher.setQuestion(record.getQuestion());
        teacher.setAnswer(record.getAnswer());
        teacher.setUtype(record.getUtype());

        if(teacherService.updateByPrimaryKey(teacher)>0){
            return ServerResponse.createBySuccess("更新数据成功",teacher);
        }else{
            return ServerResponse.createByErrorMessage("更新数据失败");
        }
    }
}